// pages/index/mypage.js
var app = getApp();
Page({
  data: {

  },
  onLoad: function() {
    var that = this;
    var token = wx.getStorageSync('token');
    if (!token) {
      app.util.newLogin(app, function (res) {
      if(res){
          wx.showLoading({ title: '数据加载中...', mask: true });
          app.func.req('users/show', {
          }, 'GET', function (res) {
            if (res.code == "success") {
              var user = res.result;
              that.setData({
                user: user
              });
            }
            wx.hideLoading();
          }, {
              'token': wx.getStorageSync('token')
            });
        }
      });
      return;
    }
     
    var that = this;
    wx.showLoading({ title: '数据加载中...', mask: true });
    app.func.req('users/show', {
    }, 'GET', function (res) {
      if (res.code == "success") {
        var user = res.result;
        that.setData({
          user: user
        });
      }
      wx.hideLoading();
    }, {
        'token': wx.getStorageSync('token')
      });

    var home_url = wx.getStorageSync('current_index_url');
    if (home_url) {
      this.setData({
        home_url: home_url
      })
    } else {
      this.setData({
        home_url: '/pages/load/load'
      })
    }
  },
  onShow: function() {
    var carts = wx.getStorageSync('carts');
    var cart_count = 0;
    var shop_id = wx.getStorageSync('shop_id');
    if (carts && shop_id > 0) {
      carts.forEach(function (cart) {
        if (cart.shopId == shop_id) {
          cart_count = cart.products.length;
        }
      })
    }
    this.setData({ cart_count: cart_count })
  }
})
